﻿using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using Models;

    public partial class AdminAdd : AdminBase
    {
        public int _Id = 0;
        private DataTable dc = new DataTable();
        public string admin_power = "";
    public string SH_power = "";
    public string admin_role = "";


        protected void Page_Load(object sender, EventArgs e)
        {

            Response.CacheControl = "no-cache";

            if (!string.IsNullOrEmpty(Request.Params["ID"]))
            {
                _Id = int.Parse(HttpContext.Current.Request.QueryString["ID"]);

            }

            if (!Page.IsPostBack)
            {

                if (_Id != 0)
                {
                    ShowInfo(_Id);
                    this.Panel2.Visible = true;
                    Literal1.Text = Convert.ToString(_Id);
                    using (Entities db = new Entities())
                    {
                    var adminuser = db.Manages.SingleOrDefault(p => p.MID == _Id);

                        admin_role = adminuser.MRole;
                        admin_power = adminuser.MPower;
                        if(adminuser.MHRPower!=null)
                        SH_power = adminuser.MHRPower;
                }
                }
                else
                {
                    this.Panel1.Visible = true;
                }
                GetMenuString();
                grdbind();

            }


        }

        //保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Entities mydb = new Entities();
            {
                Manages Lt = new Manages();
                if (_Id != 0)
                    Lt = mydb.Manages.First(p => p.MID == _Id);

                Lt.MName = txtName.Text.Trim();

                Lt.MRole = "";
                string admin_power = ",";
                if (Request.Form["admin_power"] != null)
                    admin_power = "," + Request.Form["admin_power"].ToString() + ",";
                Lt.MPower = admin_power;


            string admin_Role = ",";
                if (Request.Form["admin_Role"] != null)
                    admin_Role = "," + Request.Form["admin_Role"].ToString() + ",";
                Lt.MRole = admin_Role;



            string sh_Role = ",";
            if (Request.Form["sh_Role"] != null)
                admin_Role = "," + Request.Form["sh_Role"].ToString() + ",";
            Lt.MHRPower = sh_Role;


            if (_Id == 0)
                {
                    Lt.MPWD = DESEncrypt.Encrypt(this.txtTag.Text.ToString().Trim());
                    mydb.Manages.Add(Lt);
                    mydb.SaveChanges();
                Response.Write("<script> var Close=1;</script>");
            }
                else
                {
                    mydb.SaveChanges();
                Response.Write("<script> var Close=1;</script>");
            }
            }


     
        }

        private void ShowInfo(int _id)
        {

            Entities mydb = new Entities();
            {

                Manages _mg = mydb.Manages.First(p => p.MID == _id);
                txtName.Text = _mg.MName;


            }
        }





        protected void GetMenuString()
        {
            List<LeftMenu> menus = LeftMenu.Load();
            StringBuilder sb = new StringBuilder();
            foreach (LeftMenu menu in menus)
            {
                sb.Append("<table class=\"footable table table - stripped toggle - arrow - tiny footable - loaded\" ><tr>\r\n");
  
                sb.Append("<td  align=left class=\"menu_title\" ><input type=checkbox  name=\"admin_power\" class=\"checkall checkbox-inline i-checks\" value=\"");
                sb.Append(menu.Code + "\"");
                if (_Id != 0)
                {
                    if (admin_power.IndexOf("," + menu.Code + ",") > -1)
                        sb.Append(" checked");
                }
                sb.Append("/>");

                sb.AppendFormat("<b>{0}</b></td></tr>\r\n", menu.Title);

                sb.AppendFormat("<tr><td  align=left><ul class=\"sec_menu\" menuid=\"{0}\">\r\n", menu.Code);
                foreach (MenuLink link in menu.Links)
                {
                    sb.Append("<li  class=\"li_menu\"><input type=checkbox  name=\"admin_power\" class=\"checkall checkbox-inline i-checks\"  value=\"");
                    sb.Append(link.Code + "\"");
                    if (_Id != 0)
                    {
                        if (admin_power.IndexOf("," + link.Code + ",") > -1)
                            sb.Append(" checked");
                    }
                    sb.Append("/>");
                    sb.AppendFormat(" {0} </li>\r\n",  link.Title);
                }
                sb.Append("</ul></td></tr></table><br>\r\n");
            }
            ltMasterSetting.Text = sb.ToString();
        }

        protected void btnSaveSetting_Click(object sender, EventArgs e)
        {
            string admin_power = ",";
            if (Request.Form["admin_power"] != null)
                admin_power = "," + Request.Form["admin_power"].ToString() + ",";
            using (Entities db = new Entities())
            {
                var upadmin = db.Manages.Single(p => p.MID == int.Parse(Request.QueryString["ID"].ToString()));
                upadmin.MPower = admin_power;
                db.SaveChanges();
            }
            GetMenuString();


        }

        //  新闻类别
        public void grdbind()
        {
            using (Entities newlx = new Entities())
            {
                dc.Columns.Add("Classid", typeof(int));
                dc.Columns.Add("ClassName", typeof(string));
                dc.Columns.Add("lb", typeof(int));


                var m = from n in newlx.L_Classs where n.CpareID == 0 select n;
                foreach (var n in m)
                {
                    DataRow row = this.dc.NewRow();
                    row["Classid"] = int.Parse(n.CID.ToString());
                    row["lb"] = int.Parse(n.CType.ToString());
   

                    row["ClassName"] = "<img src='../50/images/tree_minusmiddle.gif' align='absmiddle' border='0' /><img src='../50/images/tree_folder.gif' align='absmiddle' border='0' />" +  n.CNmae.ToString() + "&nbsp;&nbsp;&nbsp;&nbsp;" + addchk(n.CID) +"发布权限</label>" + addshchk(n.CID)+ "审核权限</label>";



                //}
                dc.Rows.Add(row);
                    int leng = 1;
                    dpbinder(dc, int.Parse(n.CID.ToString()), leng);
                }
                this.rptList.DataSource = dc.DefaultView;
                rptList.DataBind();
            }
        }

        public void dpbinder(DataTable dp, int cID, int leng)//根据父ID查询子项
        {

            using (Entities newlx = new Entities())
            {

                int a;
                int.TryParse(cID.ToString(), out a);

                var mm = from nn in newlx.L_Classs where nn.CpareID == a select nn;
                leng++;
                foreach (var n in mm)
                {
                    string tr = addtab(leng);
                    DataRow row = this.dc.NewRow();
                    row["Classid"] = int.Parse(n.CID.ToString());
                    row["lb"] = int.Parse(n.CType.ToString());
                    if (n.CID.ToString() == mm.ToList().Last().CID.ToString())
                    {

                        row["ClassName"] = tr + "<img src='../50/images/tree_minusbottom.gif' align='absmiddle' border='0' /><img src='../50/images/tree_folder.gif' align='absmiddle' border='0' />" + n.CNmae.ToString() + "&nbsp;&nbsp;&nbsp;&nbsp;" + addchk(n.CID) + "发布权限</label>" + addshchk(n.CID) + "审核权限</label>";
                }
                    else
                    {
                        row["ClassName"] = tr + "<img src='../50/images/tree_minusmiddle.gif' align='absmiddle' border='0' /><img src='../50/images/tree_folder.gif' align='absmiddle' border='0' />" + n.CNmae.ToString() + "&nbsp;&nbsp;&nbsp;&nbsp;" + addchk(n.CID) + "发布权限</label>" + addshchk(n.CID) + "审核权限</label>";

                }

                    dp.Rows.Add(row);
                    dpbinder(dp, int.Parse(n.CID.ToString()), leng);



                }
            }
        }
        private string addtab(int i)
        {
            string aa = "";
            for (int j = 1; j < i; j++)
            {
                aa += "<img src='../50/images/tree_line.gif' align='absmiddle' border='0' />";
            }
            return aa;
        }

        private string addchk(long claid)
        {

            string _claid = claid.ToString();

   
            StringBuilder _mycn = new StringBuilder();
            _mycn.Append("<label class=\"checkbox-inline i-checks\">  <input type=checkbox   class=\"checkall\" name=\"admin_Role\" value=\"");
            _mycn.Append(_claid + "\"");
            if (_Id != 0)
            {
                if (admin_role.IndexOf("," + _claid + ",") > -1)
                    _mycn.Append(" checked");
            }
            else
            {
                _mycn.Append(" checked");
            }
            _mycn.Append("/>  &nbsp;");
            return _mycn.ToString();

        }


    private string addshchk(long claid)
    {

        string _claid = claid.ToString();


        StringBuilder _mycn = new StringBuilder();
        _mycn.Append("<label class=\"checkbox-inline i-checks\">  <input type=checkbox   class=\"checkall\" name=\"sh_Role\" value=\"");
        _mycn.Append(_claid + "\"");
        if (_Id != 0)
        {
            if (SH_power.IndexOf("," + _claid + ",") > -1)
                _mycn.Append(" checked");
        }
        else
        {
            _mycn.Append(" checked");
        }
        _mycn.Append("/>  &nbsp;");
        return _mycn.ToString();

    }



}



